<?php
namespace app\modules\app\models;

use app\modules\cn\models\User;
use yii\db\ActiveRecord;
use yii;

class IeltsSpeakLike extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%ielts_speak_like}}';
    }

    /**
     * 录音点赞数
     * @param $data
     * @param $uid
     * @param $type 2老师录音点赞
     * @return mixed
     */
    public static function getLikeNum($data,$uid,$type){
        foreach ($data as $k=>$v){
            if(!$v) continue;
            $num = IeltsSpeakLike::find()->where("contentId={$v['id']} and type=$type")->count('id');
            $isLike = IeltsSpeakLike::find()->select('id')->where("contentId={$v['id']} and type=$type and uid=$uid")->one();
            $data[$k]['like'] = $num;
            $data[$k]['isLike'] = $isLike?1:0;
            if($type==1){
                //用户信息
                $userid = $v['uid']?$v['uid']:0;
                $user = User::find()->where("uid=$userid")->asArray()->one();
                if(!empty($user['userName'])){
                    $data[$k]['userName'] = $user['userName'];
                }else{
                    $data[$k]['userName'] = '';
                }
                if(!empty($user['image'])){
                    $data[$k]['image'] = Yii::$app->params['thinkUrl'].$user['image'];
                }else{
                    $data[$k]['image'] = Yii::$app->params['defaultImg'];
                }
            }
        }
        return $data;
    }

}